package com.ruoyi.web.controller.flyflow.factory;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.business.base.JoSqlUtil;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.web.controller.flyflow.util.FlyFlowUtil;
import com.ruoyi.web.controller.flyflow.vo.FlyFlowTaskListVO;
import com.ruoyi.web.controller.flyflow.vo.FlyFloyCompleteTaskVO;
import com.ruoyi.web.controller.flyflow.vo.FlyFloyFormatStartNodeShowVO;
import com.ruoyi.web.controller.flyflow.vo.FlyFloyQueryHeaderShowVO;
import com.ruoyi.web.controller.thirdparty.tool.TTool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * FlyFlow 工厂
 *
 * @author Jyl
 */
@Slf4j
@Component
public class FlyFlowFactory
{
    @Autowired
    private TokenService tokenService;
    @Autowired
    private FlyFlowFnFactory fnFactory;

    protected JoSqlUtil joSqlUtil = new JoSqlUtil();

    /**
     * 获取当前节点
     */
    public JSONObject findByProcInstId(String processInstanceId)
    {
        String url = "node/findByProcInstId?procInstId={}";
        return JSONObject.parseObject(FlyFlowUtil.get(buildHeaderMap(), StrUtil.format(url, processInstanceId)));
    }

    /**
     * 获取流程详情
     */
    public JSONObject getByProcessInstanceId(String processInstanceId)
    {
        String url = "process-instance/getByProcessInstanceId?processInstanceId={}";
        return JSONObject.parseObject(FlyFlowUtil.get(buildHeaderMap(), StrUtil.format(url, processInstanceId))
        );
    }

    /**
     * 获取流程
     */
    public JSONObject getProcessInstance()
    {
        String url = "combination/group/listCurrentUserStartGroup?hidden=false";
        return JSONObject.parseObject(FlyFlowUtil.get(buildHeaderMap(), url));
    }

    /**
     * 获取流程启动节点详情
     */
    public JSONObject getProcessInstanceInfo(String flowId)
    {
        String url = "form/getFormDetail";
        JSONObject json = new JSONObject();
        json.put("flowId", flowId);
        json.put("from", "start");
        return JSONObject.parseObject(FlyFlowUtil.post(buildHeaderMap(), json, url));
    }

    /**
     * 获取审批表单详情
     */
    public JSONObject sendTak(JSONObject flyflow)
    {
        String url = "task/completeTask";
        return JSONObject.parseObject(FlyFlowUtil.post(buildHeaderMap(), flyflow, url));
    }

    /**
     * 启动流程
     */
    public JSONObject processInstanceStart(JSONObject jsonObject)
    {
        String url = "process-instance/startProcessInstance";
        String result = """
                {
                    "flowId": "#{flowId}",
                    "uniqueId": "#{uniqueId}",
                    "paramMap": #{paramMap}
                }
                """;
        result = joSqlUtil.sqlJson(result, jsonObject);
        JSONObject jsonObject1 = JSONObject.parseObject(result);
        log.info("[{}]:提交参数>>>{}", "启动流程(processInstanceStart)", jsonObject1);
        return JSONObject.parseObject(FlyFlowUtil.post(buildHeaderMap(), jsonObject1, url));
    }

    /**
     * 结束流程
     */
    public JSONObject processInstanceEnd(JSONObject jsonObject)
    {
        String url = "remote/processEndEvent";
//        String result = """
//                {
//                    "flowId": "#{flowId}",
//                    "uniqueId": "#{uniqueId}",
//                    "paramMap": #{paramMap}
//                }
//                """;
//        result = joSqlUtil.sqlJson(result, jsonObject);
//        JSONObject jsonObject1 = JSONObject.parseObject(result);
        return JSONObject.parseObject(FlyFlowUtil.post(buildHeaderMap(), jsonObject, url));
    }

    /**
     * 查询待办列表
     */
    public JSONObject queryTodoTaskList(FlyFlowTaskListVO flyFlowTaskListVO)
    {
        String url = "combination/group/queryTodoTaskList";
        JSONObject json = JSONObject.parseObject(JSONObject.toJSONString(flyFlowTaskListVO));
        return JSONObject.parseObject(FlyFlowUtil.post(buildHeaderMap(), json, url));
    }

    /**
     * 构建请求头
     */
    private Map<String, String> buildHeaderMap()
    {
        Map<String, String> params = new HashMap<>();
        params.put("Authorization", tokenService.getToken(ServletUtils.getRequest()));
        params.put("flyflow-tenant-id", "1");
        params.put("flyflowversion", "V9.9.9");
        params.put("content-type", "application/json;charset=UTF-8");
        return params;
    }

    /**
     * @param taskId 待办id
     * @return 待办详情
     */
    public JSONObject getTask(String taskId)
    {
        String url = "task/getTask?hidden=" + taskId;
        return JSONObject.parseObject(FlyFlowUtil.get(buildHeaderMap(), url));
    }

    /**
     * @param flyFloyQueryHeaderShowVO 查询vo
     * @return 待办 标题信息
     */
    public JSONObject queryHeaderShow(FlyFloyQueryHeaderShowVO flyFloyQueryHeaderShowVO)
    {
        String url = "base/queryHeaderShow";
        return JSONObject.parseObject(FlyFlowUtil.post(buildHeaderMap(), flyFloyQueryHeaderShowVO, url));
    }

    /**
     * @param flyFloyQueryHeaderShowVO 查询vo
     * @return 待办 表单信息
     */
    public JSONObject getFormDetail(FlyFloyQueryHeaderShowVO flyFloyQueryHeaderShowVO)
    {
        String url = "form/getFormDetail";
        return JSONObject.parseObject(FlyFlowUtil.post(buildHeaderMap(), flyFloyQueryHeaderShowVO, url));
    }

    /**
     * @param taskId 查询vo
     * @return 任务操作数据
     */
    public JSONObject queryTaskOperData(String taskId)
    {
        String url = "base/queryTaskOperData?taskId=" + taskId;
        return JSONObject.parseObject(FlyFlowUtil.get(buildHeaderMap(), url));
    }

    /**
     * @param flyFloyFormatStartNodeShowVO 查询vo
     * @return 流程轨迹
     */
    public JSONObject formatStartNodeShow(FlyFloyFormatStartNodeShowVO flyFloyFormatStartNodeShowVO)
    {
        String url = "base/formatStartNodeShow";
        return JSONObject.parseObject(FlyFlowUtil.post(buildHeaderMap(), flyFloyFormatStartNodeShowVO, url));
    }

    /**
     * @param flyFloyFormatStartNodeShowVO 查询vo
     * @return 流程轨迹
     */
    public String nextNode(FlyFloyFormatStartNodeShowVO flyFloyFormatStartNodeShowVO)
    {
        return fnFactory.fnNextNode(formatStartNodeShow(flyFloyFormatStartNodeShowVO));
    }

    /**
     * @param flyFloyCompleteTaskVO 提交vo
     * @return 审批结果
     */
    public JSONObject completeTask(FlyFloyCompleteTaskVO flyFloyCompleteTaskVO)
    {
        String url = "task/completeTask";
        return JSONObject.parseObject(FlyFlowUtil.post(buildHeaderMap(), flyFloyCompleteTaskVO, url));
    }

    /**
     * 刷新令牌
     */
    public JSONObject refreshToken()
    {
        String url = "login/refreshToken";
        return JSONObject.parseObject(FlyFlowUtil.post(buildHeaderMap(), TTool.jo("token",
                tokenService.getToken(ServletUtils.getRequest())), url));
    }

    public JSONObject refreshNodes(String key, String value)
    {
        String url = "remote/refreshNodes";
        return JSONObject.parseObject(FlyFlowUtil.post(buildHeaderMap(), TTool.jo("key",
                key).fluentPut("value", value), url));
    }
}
